home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / GridLayout.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  13.1 KB  |  407 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)GridLayout.java    1.23 98/09/21
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. /**
  18.  * The <code>GridLayout</code> class is a layout manager that 
  19.  * lays out a container's components in a rectangular grid. 
  20.  * <p>
  21.  * The container is divided into equal-sized rectangles, 
  22.  * and one component is placed in each rectangle. 
  23.  * <p>
  24.  * For example, the following is an applet that lays out six buttons 
  25.  * into three rows and two columns: 
  26.  * <p>
  27.  * <hr><blockquote><pre>
  28.  * import java.awt.*;
  29.  * import java.applet.Applet;
  30.  * public class ButtonGrid extends Applet {
  31.  *     public void init() {
  32.  *         setLayout(new GridLayout(3,2));
  33.  *         add(new Button("1"));
  34.  *         add(new Button("2"));
  35.  *         add(new Button("3"));
  36.  *         add(new Button("4"));
  37.  *         add(new Button("5"));
  38.  *         add(new Button("6"));
  39.  *     }
  40.  * }
  41.  * </pre></blockquote><hr>     
  42.  * <p>
  43.  * It produces the following output:
  44.  * <p>
  45.  * <img src="doc-files/GridLayout-1.gif" 
  46.  * ALIGN=center HSPACE=10 VSPACE=7>
  47.  *
  48.  * @version 1.23, 09/21/98
  49.  * @author 
  50.  * @since   JDK1.0
  51.  */
  52. public class GridLayout implements LayoutManager, java.io.Serializable {
  53.     /**
  54.      * This is the horizontal gap (in pixels) which specifies the space
  55.      * between columns.  They can be changed at any time.
  56.      * This should be a non negative integer.
  57.      *
  58.      * @serial
  59.      * @see getHgap()
  60.      * @see setHgap()
  61.      */
  62.     int hgap;
  63.     /**
  64.      * This is the vertical gap (in pixels) which specifies the space
  65.      * between rows.  They can be changed at any time.
  66.      * This should be a non negative integer.
  67.      *
  68.      * @serial
  69.      * @see getVgap()
  70.      * @see setVgap()
  71.      */
  72.     int vgap;
  73.     /**
  74.      * This is the number of rows specified for the grid.  The number
  75.      * of rows can be changed at any time.
  76.      * This should be a non negative integer, where '0' means
  77.      * 'any number' meaning that the number of Rows in that
  78.      * dimension depends on the other dimension.
  79.      *
  80.      * @serial
  81.      * @see getRows()
  82.      * @see setRows()
  83.      */
  84.     int rows;
  85.     /**
  86.      * This is the number of columns specified for the grid.  The number
  87.      * of columns can be changed at any time.
  88.      * This should be a non negative integer, where '0' means
  89.      * 'any number' meaning that the number of Columns in that
  90.      * dimension depends on the other dimension.
  91.      *
  92.      * @serial
  93.      * @see getColumns()
  94.      * @see setColumns()
  95.      */
  96.     int cols;
  97.  
  98.     /**
  99.      * Creates a grid layout with a default of one column per component,
  100.      * in a single row.
  101.      * @since JDK1.1
  102.      */
  103.     public GridLayout() {
  104.     this(1, 0, 0, 0);
  105.     }
  106.  
  107.     /**
  108.      * Creates a grid layout with the specified number of rows and 
  109.      * columns. All components in the layout are given equal size. 
  110.      * <p>
  111.      * One, but not both, of <code>rows</code> and <code>cols</code> can 
  112.      * be zero, which means that any number of objects can be placed in a 
  113.      * row or in a column. 
  114.      * @param     rows   the rows, with the value zero meaning 
  115.      *                   any number of rows.
  116.      * @param     cols   the columns, with the value zero meaning 
  117.      *                   any number of columns.
  118.      */
  119.     public GridLayout(int rows, int cols) {
  120.     this(rows, cols, 0, 0);
  121.     }
  122.  
  123.     /**
  124.      * Creates a grid layout with the specified number of rows and 
  125.      * columns. All components in the layout are given equal size. 
  126.      * <p>
  127.      * In addition, the horizontal and vertical gaps are set to the 
  128.      * specified values. Horizontal gaps are placed at the left and 
  129.      * right edges, and between each of the columns. Vertical gaps are 
  130.      * placed at the top and bottom edges, and between each of the rows. 
  131.      * <p>
  132.      * One, but not both, of <code>rows</code> and <code>cols</code> can 
  133.      * be zero, which means that any number of objects can be placed in a 
  134.      * row or in a column. 
  135.      * @param     rows   the rows, with the value zero meaning 
  136.      *                   any number of rows.
  137.      * @param     cols   the columns, with the value zero meaning 
  138.      *                   any number of columns.
  139.      * @param     hgap   the horizontal gap. 
  140.      * @param     vgap   the vertical gap. 
  141.      * @exception   IllegalArgumentException  if the of <code>rows</code> 
  142.      *                   or <code>cols</code> is invalid.
  143.      */
  144.     public GridLayout(int rows, int cols, int hgap, int vgap) {
  145.     if ((rows == 0) && (cols == 0)) {
  146.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  147.     }
  148.     this.rows = rows;
  149.     this.cols = cols;
  150.     this.hgap = hgap;
  151.     this.vgap = vgap;
  152.     }
  153.  
  154.     /**
  155.      * Gets the number of rows in this layout.
  156.      * @return    the number of rows in this layout.
  157.      * @since     JDK1.1
  158.      */
  159.     public int getRows() {
  160.     return rows;
  161.     }
  162.  
  163.     /**
  164.      * Sets the number of rows in this layout to the specified value.
  165.      * @param        rows   the number of rows in this layout.
  166.      * @exception    IllegalArgumentException  if the value of both 
  167.      *               <code>rows</code> and <code>cols</code> is set to zero.
  168.      * @since        JDK1.1
  169.      */
  170.     public void setRows(int rows) {
  171.     if ((rows == 0) && (this.cols == 0)) {
  172.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  173.     }
  174.     this.rows = rows;
  175.     }
  176.  
  177.     /**
  178.      * Gets the number of columns in this layout.
  179.      * @return     the number of columns in this layout.
  180.      * @since      JDK1.1
  181.      */
  182.     public int getColumns() {
  183.     return cols;
  184.     }
  185.  
  186.     /**
  187.      * Sets the number of columns in this layout to the specified value.
  188.      * @param        cols   the number of columns in this layout.
  189.      * @exception    IllegalArgumentException  if the value of both 
  190.      *               <code>rows</code> and <code>cols</code> is set to zero.
  191.      * @since        JDK1.1
  192.      */
  193.     public void setColumns(int cols) {
  194.     if ((cols == 0) && (this.rows == 0)) {
  195.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  196.     }
  197.     this.cols = cols;
  198.     }
  199.  
  200.     /**
  201.      * Gets the horizontal gap between components.
  202.      * @return       the horizontal gap between components.
  203.      * @since        JDK1.1
  204.      */
  205.     public int getHgap() {
  206.     return hgap;
  207.     }
  208.     
  209.     /**
  210.      * Sets the horizontal gap between components to the specified value.
  211.      * @param        hgap   the horizontal gap between components.
  212.      * @since        JDK1.1
  213.      */
  214.     public void setHgap(int hgap) {
  215.     this.hgap = hgap;
  216.     }
  217.     
  218.     /**
  219.      * Gets the vertical gap between components.
  220.      * @return       the vertical gap between components.
  221.      * @since        JDK1.1
  222.      */
  223.     public int getVgap() {
  224.     return vgap;
  225.     }
  226.     
  227.     /**
  228.      * Sets the vertical gap between components to the specified value.
  229.      * @param         vgap  the vertical gap between components.
  230.      * @since        JDK1.1
  231.      */
  232.     public void setVgap(int vgap) {
  233.     this.vgap = vgap;
  234.     }
  235.  
  236.     /**
  237.      * Adds the specified component with the specified name to the layout.
  238.      * @param name the name of the component.
  239.      * @param comp the component to be added.
  240.      */
  241.     public void addLayoutComponent(String name, Component comp) {
  242.     }
  243.  
  244.     /**
  245.      * Removes the specified component from the layout. 
  246.      * @param comp the component to be removed.
  247.      */
  248.     public void removeLayoutComponent(Component comp) {
  249.     }
  250.  
  251.     /** 
  252.      * Determines the preferred size of the container argument using 
  253.      * this grid layout. 
  254.      * <p>
  255.      * The preferred width of a grid layout is the largest preferred 
  256.      * width of any of the widths in the container times the number of 
  257.      * columns, plus the horizontal padding times the number of columns 
  258.      * plus one, plus the left and right insets of the target container. 
  259.      * <p>
  260.      * The preferred height of a grid layout is the largest preferred 
  261.      * height of any of the heights in the container times the number of 
  262.      * rows, plus the vertical padding times the number of rows plus one, 
  263.      * plus the top and bottom insets of the target container. 
  264.      * 
  265.      * @param     target   the container in which to do the layout.
  266.      * @return    the preferred dimensions to lay out the 
  267.      *                      subcomponents of the specified container.
  268.      * @see       java.awt.GridLayout#minimumLayoutSize 
  269.      * @see       java.awt.Container#getPreferredSize()
  270.      */
  271.     public Dimension preferredLayoutSize(Container parent) {
  272.       synchronized (parent.getTreeLock()) {
  273.     Insets insets = parent.getInsets();
  274.     int ncomponents = parent.getComponentCount();
  275.     int nrows = rows;
  276.     int ncols = cols;
  277.  
  278.     if (nrows > 0) {
  279.         ncols = (ncomponents + nrows - 1) / nrows;
  280.     } else {
  281.         nrows = (ncomponents + ncols - 1) / ncols;
  282.     }
  283.     int w = 0;
  284.     int h = 0;
  285.     for (int i = 0 ; i < ncomponents ; i++) {
  286.         Component comp = parent.getComponent(i);
  287.         Dimension d = comp.getPreferredSize();
  288.         if (w < d.width) {
  289.         w = d.width;
  290.         }
  291.         if (h < d.height) {
  292.         h = d.height;
  293.         }
  294.     }
  295.     return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
  296.                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
  297.       }
  298.     }
  299.  
  300.     /**
  301.      * Determines the minimum size of the container argument using this 
  302.      * grid layout. 
  303.      * <p>
  304.      * The minimum width of a grid layout is the largest minimum width 
  305.      * of any of the widths in the container times the number of columns, 
  306.      * plus the horizontal padding times the number of columns plus one, 
  307.      * plus the left and right insets of the target container. 
  308.      * <p>
  309.      * The minimum height of a grid layout is the largest minimum height 
  310.      * of any of the heights in the container times the number of rows, 
  311.      * plus the vertical padding times the number of rows plus one, plus 
  312.      * the top and bottom insets of the target container. 
  313.      *  
  314.      * @param       target   the container in which to do the layout.
  315.      * @return      the minimum dimensions needed to lay out the 
  316.      *                      subcomponents of the specified container.
  317.      * @see         java.awt.GridLayout#preferredLayoutSize
  318.      * @see         java.awt.Container#doLayout
  319.      */
  320.     public Dimension minimumLayoutSize(Container parent) {
  321.       synchronized (parent.getTreeLock()) {
  322.         Insets insets = parent.getInsets();
  323.     int ncomponents = parent.getComponentCount();
  324.     int nrows = rows;
  325.     int ncols = cols;
  326.  
  327.     if (nrows > 0) {
  328.         ncols = (ncomponents + nrows - 1) / nrows;
  329.     } else {
  330.         nrows = (ncomponents + ncols - 1) / ncols;
  331.     }
  332.     int w = 0;
  333.     int h = 0;
  334.     for (int i = 0 ; i < ncomponents ; i++) {
  335.         Component comp = parent.getComponent(i);
  336.         Dimension d = comp.getMinimumSize();
  337.         if (w < d.width) {
  338.         w = d.width;
  339.         }
  340.         if (h < d.height) {
  341.         h = d.height;
  342.         }
  343.     }
  344.     return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
  345.                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
  346.       }
  347.     }
  348.  
  349.     /** 
  350.      * Lays out the specified container using this layout. 
  351.      * <p>
  352.      * This method reshapes the components in the specified target 
  353.      * container in order to satisfy the constraints of the 
  354.      * <code>GridLayout</code> object. 
  355.      * <p>
  356.      * The grid layout manager determines the size of individual 
  357.      * components by dividing the free space in the container into 
  358.      * equal-sized portions according to the number of rows and columns 
  359.      * in the layout. The container's free space equals the container's 
  360.      * size minus any insets and any specified horizontal or vertical 
  361.      * gap. All components in a grid layout are given the same size. 
  362.      *  
  363.      * @param      target   the container in which to do the layout.
  364.      * @see        java.awt.Container
  365.      * @see        java.awt.Container#doLayout
  366.      */
  367.     public void layoutContainer(Container parent) {
  368.       synchronized (parent.getTreeLock()) {
  369.     Insets insets = parent.getInsets();
  370.     int ncomponents = parent.getComponentCount();
  371.     int nrows = rows;
  372.     int ncols = cols;
  373.  
  374.     if (ncomponents == 0) {
  375.         return;
  376.     }
  377.     if (nrows > 0) {
  378.         ncols = (ncomponents + nrows - 1) / nrows;
  379.     } else {
  380.         nrows = (ncomponents + ncols - 1) / ncols;
  381.     }
  382.     int w = parent.width - (insets.left + insets.right);
  383.     int h = parent.height - (insets.top + insets.bottom);
  384.     w = (w - (ncols - 1) * hgap) / ncols;
  385.     h = (h - (nrows - 1) * vgap) / nrows;
  386.  
  387.     for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) {
  388.         for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
  389.         int i = r * ncols + c;
  390.         if (i < ncomponents) {
  391.             parent.getComponent(i).setBounds(x, y, w, h);
  392.         }
  393.         }
  394.     }
  395.       }
  396.     }
  397.     
  398.     /**
  399.      * Returns the string representation of this grid layout's values.
  400.      * @return     a string representation of this grid layout.
  401.      */
  402.     public String toString() {
  403.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + 
  404.                            ",rows=" + rows + ",cols=" + cols + "]";
  405.     }
  406. }
  407.